<?php

class DataGrabberController extends Zend_Controller_Action
{
    public function init() 
    {
        /*
        $config = Zend_Registry::getInstance()->configuration;
        
        $rootFolder = isset($config->datagrabber->folders->grabs) ? $config->datagrabber->folders->grabs : null;
        $wowheadFolder = isset($config->datagrabber->folders->wowhead) ? $config->datagrabber->folders->wowhead : null;
        
        if (!$rootFolder) {
            throw new Exception('Datagrabber folder is not defined in config file. Please do so under: datagrabber.folders.grabs');
        } 
        
        if (!is_writable($rootFolder)) {
            throw new Exception('Datagrabber folder: ' . $rootFolder . ' is not writeable. ');
        }
        
        if (!$wowheadFolder) {
            throw new Exception('Wowhead folder is not defined in config file. Please do so under: datagrabber.folders.wowhead');
        } 
        
        if (!is_writable($wowheadFolder)) {
            throw new Exception('Wowhead folder: ' . $wowheadFolder . ' is not writeable. ');
        }
        */
    }

    public function indexAction()
    {
        $grabsTable = new DataGrabber_Grabs();
        $grabs = $grabsTable->fetchAll();
        
        $this->view->grabs = $grabs;
    } 
    
    public function showAction()
    {
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grab'));
        
        if (!$grab) {
            return;
        }
        
        $this->view->grab = $grab;
    }
    
    public function transferAction()
    {
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grab'));
        
        if (!$grab) {
            return;
        }
        
        $this->view->grab = $grab;
        
        try {
            $grab->transferToDatabase(true);
        } catch (Exception $e) {
            $this->view->message = $e->getMessage();
        }
        
        if ($this->_request->isPost()) {
            
            try {
                $grab->transferToDatabase(false);
                $this->view->message = 'Update complete';
            } catch (Exception $e) {
                //throw $e;
                
                echo $e->getMessage();
                echo "<br/>";
                echo $e->getTraceAsString();
            }
        }
    }
    
    public function clearInitialImportAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        
        if (!$this->_request->isPost()){
            echo $this->_helper->jsonError('Request is not Post!');
            return;
        }
        
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grabId'));
        if (!$grab) {
            echo $this->_helper->jsonError('Invalid Grab');
            return;
        }
        
        $grab->clearImportedData();
        echo $this->_helper->jsonResponse("ok");
    }
    
    public function fetchNextLootTableAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        
        if (!$this->_request->isPost()){
            echo $this->_helper->jsonError('Request is not Post!');
            return;
        }
        
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grabId'));
        if (!$grab) {
            echo $this->_helper->jsonError('Invalid Grab');
            return;
        }
        
        $count = (int)DataGrabber_Grabs::getById($this->_request->getParam('count'));
        if ($count == 0) {
            $count = 5;
        }
        
        $grab->initializeData();
        
        try {
            
            for($i = 0; $i < $count; $i++) {
                $moreTables = $grab->fetchNextLootTable();
                if(!$moreTables) {
                    echo $this->_helper->jsonError('No more loot tables to fetch.');
                    return;
                }
            }
            $info = $grab->getInfo();
            echo $this->_helper->jsonResponse($info['lootTables']); 

        } catch (Exception $e) {
            echo $this->_helper->jsonError($e->getMessage());
        }
    }
    
    public function fetchNextItemAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        
        if (!$this->_request->isPost()){
            echo $this->_helper->jsonError('Request is not Post!');
            return;
        }
        
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grabId'));
        if (!$grab) {
            echo $this->_helper->jsonError('Invalid Grab');
            return;
        }
        
        $count = (int)DataGrabber_Grabs::getById($this->_request->getParam('count'));
        if ($count == 0) {
            $count = 10;
        }
        
        $grab->initializeData();
        
        try {
            for($i = 0; $i < $count; $i++) {
                $moreItems = $grab->fetchNextItem();
                if(!$moreItems) {
                    echo $this->_helper->jsonError('No more items to fetch.');
                    return;
                }
            }
            $info = $grab->getInfo();
            echo $this->_helper->jsonResponse($info['items']); 
        } catch (Exception $e) {
            echo $this->_helper->jsonError($e->getMessage());
        }
    }
    
    public function importInitialDataAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        
        if (!$this->_request->isPost()){
            echo $this->_helper->jsonError('Request is not Post!');
            return;
        }
        
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grabId'));
        if (!$grab) {
            echo $this->_helper->jsonError('Invalid Grab');
            return;
        }
        
        try {
            $grab->buildInitialXml();
            echo $this->_helper->jsonResponse('ok');
        } catch (Exception $e) {
            echo $this->_helper->jsonError($e->message);
        }

    }
    
    /*
     * Ajax action to get the grab information
     */
    public function getGrabInfoAction() 
    {
        $this->_helper->viewRenderer->setNoRender();
        
        if (!$this->_request->isPost()){
            echo $this->_helper->jsonError('Request is not Post!');
            return;
        }
        
        $grab = DataGrabber_Grabs::getById($this->_request->getParam('grabId'));
        if (!$grab) {
            echo $this->_helper->jsonError('Invalid Grab');
            return;
        }
        
        $grab->initializeData();
        
        echo $this->_helper->jsonResponse($grab->getInfo());
    }
}